package com.ktgj.train.controller;

import com.ktgj.common.core.web.domain.AjaxResult;
import com.ktgj.common.log.annotation.SignatureValidation;
import com.ktgj.train.common.TrainTickets;
import com.ktgj.train.domain.InterfaceEntity.*;
import com.ktgj.train.domain.SeatPrice;
import com.ktgj.train.domain.trainlist.TrainList;
import com.ktgj.train.domain.vo.CustomerTrain;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Map;

@ControllerAdvice
@RestController
@RequestMapping("/trainTickets")
public class TrainTicketsController {

    @Autowired
    private TrainTickets trainTickets;

    /**
     * 余票查询
     * @param request
     * @return
     */
    @PostMapping("/queryByTrainTickets")
    public TrRemainingTicketResponse queryByTrainTickets(@RequestBody TrRemainingTicketRequest request) {
        TrRemainingTicketResponse response = trainTickets.queryByTrainTickets(request);
        if (response != null) {
            return response;
        } else {
            return null;
        }
    }

    /**
     * 获取车次详情
     * @param request
     * @return
     */
    @PostMapping("/queryTrainSeat")
    public AjaxResult queryTrainSeat(@RequestBody QueryTrainSeatRequest request) {
        TrainList response = trainTickets.queryTrainSeat(request);
        if (response != null) {
            return AjaxResult.success(response);
        } else {
            return AjaxResult.success("没有查询到");
        }
    }

    /**
     * 获取指定座位价格
     * @param price
     * @return
     */
    @PostMapping("/seatPrice")
    public BigDecimal seatPrice(@RequestBody SeatPrice price) {
        BigDecimal seatPrice = trainTickets.seatPrice(price);
        return seatPrice;
    }

    /**
     * 订单提交（申请占座）
     * @param request
     * @return
     */
    @PostMapping("/orderSubmission")
    public AjaxResult orderSubmission(@RequestBody TrOrderSubmissionRequest request) {
        return AjaxResult.success(trainTickets.orderSubmission(request));
    }

    /**
     * 确认出票
     * @param request
     * @return
     */
    @PostMapping("/outTicket")
    public AjaxResult outTicket(@RequestBody TrOutTicketRequest request) {
        return AjaxResult.success(trainTickets.outTicket(request));
    }

    /**
     * 取消订单
     * @param request
     * @return
     */
    @PostMapping("/cancellationOrder")
    public AjaxResult cancellationOrder(@RequestBody TrCancellationOrderRequest request) {
        return AjaxResult.success(trainTickets.cancellationOrder(request));
    }

    /**
     * 订单信息查询
     * @param request
     * @return
     */
    @PostMapping("/orderQuery")
    public AjaxResult orderQuery(@RequestBody TrOrderQueryRequest request) {
        return AjaxResult.success(trainTickets.orderQuery(request));
    }

    /**
     * 获取支付链接给客人支付
     * @param request
     * @return
     */
    @PostMapping("/trainPayment")
    public AjaxResult trainPayment(@RequestBody TrainPaymentRequest request) {
        return AjaxResult.success(trainTickets.trainPayment(request));
    }

    /**
     * 支付状态同步
     * @param request
     * @return
     */
    @PostMapping("/payStatusSynch")
    public AjaxResult payStatusSynch(@RequestBody TrPayStatusSynchRequest request) {
        return AjaxResult.success(trainTickets.payStatusSynch(request));
    }

    /**
     * 请求改签
     * @param request
     * @return
     */
    @PostMapping("/trainRequestChange")
    public AjaxResult trainRequestChange(@RequestBody TrTicketChangesRequest request) {
        return AjaxResult.success(trainTickets.trainRequestChange(request));
    }

    /**
     * 获取列车经停信息
     * @param request
     * @return
     */
    @PostMapping("/trainTransit")
    public TrainTransitResponse trainTransit(@RequestBody TrainTransitRequest request) {
        TrainTransitResponse response = trainTickets.trainTransit(request);
        if (response != null) {
            return response;
        } else {
            return null;
        }
    }

    /**
     * 12306账号验证（登录）
     * @param request
     * @return
     */
    //@SignatureValidation
    @PostMapping("/relationValidate")
    public AjaxResult relationValidate(HttpServletRequest servletRequest, @RequestBody RelationValidateRequest request) {
        String token = servletRequest.getHeader("token");
        RelationValidateResponse response = null;
        if (token != null)
            response = trainTickets.relationValidate(token, request);
        else
            response = trainTickets.relationValidate(null, request);
        return AjaxResult.success(response);
    }

    /**
     * 取消改签
     * @param request
     * @return
     */
    @PostMapping("/trainCancelChange")
    public AjaxResult trainCancelChange(@RequestBody TrCancelChangeRequest request) {
        return AjaxResult.success(trainTickets.trainCancelChange(request));
    }

    /**
     * 确认改签
     * @param request
     * @return
     */
    @PostMapping("/confirmChange")
    public AjaxResult confirmChange(@RequestBody TrConfirmChangeRequest request) {
        return AjaxResult.success(trainTickets.confirmChange(request));
    }

    /**
     * 线上退票申请
     * @param request
     * @return
     */
    @PostMapping("/retreatTicket")
    public AjaxResult retreatTicket(@RequestBody TrRetreatTicketRequest request) {
        return AjaxResult.success(trainTickets.retreatTicket(request));
    }

    /**
     * 查询12306常用联系人
     * @param request
     * @return
     */
    @PostMapping("/contactQuery")
    public AjaxResult contactQuery(@RequestBody TrContactQueryRequest request) {
        return AjaxResult.success(trainTickets.contactQuery(request));
    }

    /**
     * 查询12306常用联系人（远程调用）
     * @param request
     * @return
     */
    @PostMapping("/contactQuerys")
    public TrContactQueryResponse contactQuerys(@RequestBody TrContactQueryRequest request) {
        TrContactQueryResponse response = trainTickets.contactQuery(request);
        return response;
    }

    /**
     * 增加和修改常用联系人（新版）
     * @param request
     * @return
     */
    @PostMapping("/addAndUpdateContacts")
    public AjaxResult addAndUpdateContacts(@RequestBody TrAddAndUpdateContactsRequest request) {
        return AjaxResult.success(trainTickets.addAndUpdateContacts(request));
    }

    /**
     * 删除常用联系人
     * @param request
     * @return
     */
    @PostMapping("/deleteContacts")
    public AjaxResult deleteContacts(@RequestBody TrDeleteContactsRequest request) {
        return AjaxResult.success(trainTickets.deleteContacts(request));
    }

    /**
     * 获取历史订单
     * @param request
     * @return
     */
    @PostMapping("/historyOrder")
    public AjaxResult historyOrder(@RequestBody TrHistoryOrderRequest request) {
        return AjaxResult.success(trainTickets.historyOrder(request));
    }

    /**
     * 账户余额查询
     * @return
     */
    @PostMapping("/queryMoney")
    public AjaxResult queryMoney() {
        return AjaxResult.success(trainTickets.queryMoney());
    }

    /**
     * 账号注册
     * @param request
     * @return
     */
    @PostMapping("/registerAccount")
    public TrRegisterAccountResponse registerAccount(@RequestBody TrRegisterAccountRequest request) {
        TrRegisterAccountResponse response = trainTickets.registerAccount(request);
        return response;
    }

    /**
     * 短信验证
     * @param request
     * @return
     */
    @PostMapping("/phoneMessageValidate")
    public AjaxResult phoneMessageValidate(@RequestBody TrPhoneMessageValidateRequest request) {
        return AjaxResult.success(trainTickets.phoneMessageValidate(request));
    }

    /**
     * 找回密码（获取验证码）
     * @param request
     * @return
     */
    @PostMapping("/retrievePassword")
    public AjaxResult retrievePassword(@RequestBody TrRetrievePasswordRequest request) {
        return AjaxResult.success(trainTickets.retrievePassword(request));
    }

    /**
     * 找回密码（回填验证码）
     * @param request
     * @return
     */
    @PostMapping("/sendcaptcharesult")
    public AjaxResult sendcaptcharesult(@RequestBody TrRetrievePasswordRequest request) {
        return AjaxResult.success(trainTickets.sendcaptcharesult(request));
    }

    /**
     * 重置密码（获取验证码）
     * @param request
     * @return
     */
    @PostMapping("/resetPassword")
    public AjaxResult resetPassword(@RequestBody TrResetPasswordRequest request) {
        return AjaxResult.success(trainTickets.resetPassword(request));
    }

    /**
     * 重置密码（发送验证码）
     * @param request
     * @return
     */
    @PostMapping("/resetPasswordSend")
    public AjaxResult resetPasswordSend(@RequestBody TrResetPasswordRequest request) {
        return AjaxResult.success(trainTickets.resetPasswordSend(request));
    }

    /**
     * 修改账号本人信息
     * @param request
     * @return
     */
    @PostMapping("/modifyUserInfo")
    public AjaxResult modifyUserInfo(@RequestBody TrRetrievePasswordRequest request) {
        return AjaxResult.success(trainTickets.modifyUserInfo(request));
    }

    /**
     * 退票前置核验
     * @param request
     * @return
     */
    @PostMapping("/refundPreCheck")
    public AjaxResult refundPreCheck(@RequestBody TrRefundPreCheckRequest request) {
        return AjaxResult.success(trainTickets.refundPreCheck(request));
    }

    /**
     * 乘客人核验结果查询接口(查询刷新核验状态)
     * @param request
     * @return
     */
    @PostMapping("/verificationStatus")
    public AjaxResult verificationStatus(@RequestBody TrVerificationStatusRequest request) {
        return AjaxResult.success(trainTickets.verificationStatus(request));
    }
}
